﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Inventor;

namespace CustomCommand
{
    internal partial class LayoutDlg : Form
    {
        private Inventor.Application m_application;

        AssemblyDocument doc;
        private LayoutCmd m_Cmd;
        public string m_CenterDis;
        public string m_startDis;
        public string m_Dis;
        public string m_rackExtents;
        public int m_rackExtentsDirection;
        public string filename;
        public LayoutDlg(Inventor.Application app, LayoutCmd cmd)
        {
            InitializeComponent();
            m_Cmd = cmd;
            m_application = app;

            buttonOK.Enabled = false;
            //checkBoxFace.Checked = true;
            m_CenterDis = this.textBoxCenterDis.Text;
            m_startDis = this.textBoxStartDis.Text;
            m_Dis = this.textBoxDis.Text;
            filename = this.textBox1.Text;
            m_rackExtents = this.textBoxExtentsEdit.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = this.openFileDialog1.FileName;
                filename = this.openFileDialog1.FileName;
            }

        }

        private void Layout_Load(object sender, EventArgs e)
        {
            doc = m_application.ActiveDocument as AssemblyDocument;
        }

        private void checkBoxFace_CheckedChanged(object sender, EventArgs e)
        {
            OnSelectRackFaceButton(sender, e);
        }
        private void OnSelectRackFaceButton(object sender, System.EventArgs e)
        {
            //disable rack direction selection button
            checkBoxDirection.Checked = false;

            //if selected then start face selection
            if (checkBoxFace.Checked)
            {
                m_Cmd.EnableInteraction();
            }
            else
            {
                m_Cmd.DisableInteraction();
            }
        }

        private void OnChangeDisEdit(object sender, System.EventArgs e)
        {
            //reject invalid input
            if (!ValidateIntInput(textBoxDis.Text.Trim()))
            {
                textBoxDis.ForeColor = System.Drawing.Color.Red;
                buttonOK.Enabled = false;
            }
            else //input for this parameter is validated
            {
                textBoxDis.ForeColor = System.Drawing.Color.Black;
                m_Dis = textBoxDis.Text;
                m_Cmd.UpdateCommandStatus();
            }
        }

        private void OnChangeCenterDisEdit(object sender, System.EventArgs e)
        {
            double dHeight = m_Cmd.GetValueFromExpression(textBoxCenterDis.Text.Trim());
            //reject invalid input
            if (!ValidateFloatInput(dHeight.ToString()))
            {
                textBoxCenterDis.ForeColor = System.Drawing.Color.Red;
                buttonOK.Enabled = false;
            }
            else //input for this parameter is validated
            {
                textBoxCenterDis.ForeColor = System.Drawing.Color.Black;
                m_CenterDis = textBoxCenterDis.Text;
                m_Cmd.UpdateCommandStatus();
            }
        }

        private void OnChangeStartDisEdit(object sender, System.EventArgs e)
        {
            double dWidth = m_Cmd.GetValueFromExpression(textBoxStartDis.Text.Trim());
            //reject invalid input
            if (!ValidateFloatInput(dWidth.ToString()))
            {
                textBoxStartDis.ForeColor = System.Drawing.Color.Red;
                buttonOK.Enabled = false;
            }
            else //input for this parameter is validated
            {
                textBoxStartDis.ForeColor = System.Drawing.Color.Black;
                m_startDis = textBoxStartDis.Text;
                m_Cmd.UpdateCommandStatus();
            }
        }

        private void OnChangeRackExtentsEdit(object sender, System.EventArgs e)
        {
            double dExtent = m_Cmd.GetValueFromExpression(textBoxExtentsEdit.Text.Trim());
            //reject invalid input
            if (!ValidateFloatInput(dExtent.ToString()))
            {
                textBoxExtentsEdit.ForeColor = System.Drawing.Color.Red;
                buttonOK.Enabled = false;
            }
            else
            {
                //input for this parameter is validated
                textBoxExtentsEdit.ForeColor = System.Drawing.Color.Black;
                m_rackExtents = textBoxExtentsEdit.Text;
                m_Cmd.UpdateCommandStatus();
            }
        }

        private void OnRackNegativeExtentsButton(object sender, System.EventArgs e)
        {
            m_rackExtentsDirection = 0;
            m_Cmd.UpdateCommandStatus();
        }

        private void OnRackPositiveExtentsButton(object sender, System.EventArgs e)
        {
            m_rackExtentsDirection = 1;
            m_Cmd.UpdateCommandStatus();
        }

        private void OnRackSymmetricExtentsButton(object sender, System.EventArgs e)
        {
            m_rackExtentsDirection = 2;
            m_Cmd.UpdateCommandStatus();
        }

        private void OnOK(object sender, System.EventArgs e)
        {
            m_Cmd.ExecuteCommand();
        }

        private void OnCancel(object sender, System.EventArgs e)
        {
            m_Cmd.StopCommand();
        }

        private void OnClose(object sender, System.EventArgs e)
        {
            m_Cmd.StopCommand();
        }

        private void OnHelpButton(object sender, System.EventArgs e)
        {
            //get the help manager object and display the start page of the "Inventor API" help
            m_application.HelpManager.DisplayHelpTopic("ADMAPI_12_0.chm", "");
        }

        private bool ValidateIntInput(string input)
        {
            try
            {
                int num = int.Parse(input);
                if (num <= 0)
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        private bool ValidateFloatInput(string input)
        {
            try
            {
                float num = float.Parse(input);
                if (num <= 0)
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool AllParametersAreValid()
        {
            return (
                    (textBoxDis.ForeColor != System.Drawing.Color.Red)
                    &&
                    (textBoxCenterDis.ForeColor != System.Drawing.Color.Red)
                    &&
                    (textBoxStartDis.ForeColor != System.Drawing.Color.Red)
                    &&
                    (textBoxExtentsEdit.ForeColor != System.Drawing.Color.Red)
                   );
        }

    }
}
